package com.example.cgpsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.cgpsys.beans.Order;
import com.example.cgpsys.beans.Result;
import com.example.cgpsys.dao.OrderDao;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/order")
@RestController
public class OrderController {

    private final OrderDao orderDao;

    public OrderController(OrderDao orderDao) {
        this.orderDao = orderDao;
    }

    @PostMapping("/add")
    public Result addOrder(@RequestBody Order order){
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("name",order.getName());
        orderQueryWrapper.eq("username",order.getUsername());
        if (orderDao.selectCount(orderQueryWrapper)>=1){
            return Result.error("请勿重复拼单");
        }
        orderDao.insert(order);
        return Result.success();
    }

    @GetMapping("/loadList")
    public Result loadOrderList(Integer status){
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        if (status!=null){
            orderQueryWrapper.eq("status",status);
        }
        List<Order> orderList = orderDao.selectList(orderQueryWrapper);
        return Result.success(orderList);
    }

    @PostMapping("/update")
    public Result updateOrder(@RequestBody Order order){
        orderDao.updateById(order);
        return Result.success();
    }
}
